﻿using System;
using System.Xml.Linq;
using ClrExtensions.Net.Rest;
using MediaPortal.GUI.Library;

namespace MyNetflix2
{
    public class InstantQueue : Queue
    {

        private User _user = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="user"></param>
        public InstantQueue(User user) : base(user)
        { _user = user; }

        /// <summary>
        /// Request a resource
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public override void Refresh()
        {

            //--------------------------------------------------------------------------------
            // Set up the Rest call to get the Instant Queue
            //--------------------------------------------------------------------------------
            RestCall request = RestCalls.GetQueueInstant(base.NetFlixUser.Token, StartIndex, EndIndex);

            try
            {
                //--------------------------------------------------------------------------------
                // Get the Instant Queue from Netflix
                //--------------------------------------------------------------------------------
                string xml = base.NetFlixUser.NetflixConnection.RequestResource(request, base.NetFlixUser.Token);

                //--------------------------------------------------------------------------------
                // Parse to Linq
                //--------------------------------------------------------------------------------
                XElement root = XElement.Parse(xml);

                //--------------------------------------------------------------------------------
                // Get the ETag so we can add items to the Queue later
                //--------------------------------------------------------------------------------
                ETag = root.Element("etag").Value;

                //--------------------------------------------------------------------------------
                // Add the elements to the QueueItems class
                //--------------------------------------------------------------------------------
                foreach (XElement node in root.Elements("queue_item"))
                {
                    QueueItems.Add(new QueueItem(node, _user));
                }
            }
            catch (Exception ex)
            {
                Log.Error("My NetFlix - InstantQueue:Refresh(): Error - " + ex.Message);
            }
        }

        /// <summary>
        /// Add a movie to the users Instant queue
        /// </summary>
        /// <param name="id"></param>
        /// <param name="format"></param>
        /// <param name="position"></param>
        public override void Add(string id, RestCalls.DiscFormat format, int position)
        {
            //--------------------------------------------------------------------------------
            // Lets refresh the queue first to get a new ETag to allow us to write back to NF
            //--------------------------------------------------------------------------------
            Refresh();

            //--------------------------------------------------------------------------------
            // Set up the Rest call to post to users queue
            //--------------------------------------------------------------------------------
            RestCall request = RestCalls.PostUsersQueueInstant(base.NetFlixUser.Token, id, format, position, ETag);

            //--------------------------------------------------------------------------------
            // Make the call to NetFlix and get the return inforomation
            //--------------------------------------------------------------------------------
            string xml = base.NetFlixUser.NetflixConnection.RequestResource(request, base.NetFlixUser.Token);
        }
    }
}
